﻿using Common.Exceptions;
using Microsoft.AspNetCore.Mvc;
using OpenIddict.Client;
using OrderService.Services;
using ProductService.Services;
using ProductService.Services.Dtos;
using SeckillService.Services;
using SeckillService.Services.Dtos;
using Servicecomb.Saga.Omega.Abstractions.Transaction;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.ObjectMapping;
using WebSite_Aggregate.Services.Dtos;
using YDT.Commons;

namespace ProductService.Controllers;

/// <summary>
/// 秒杀详情控制器(秒杀详情接口)
/// </summary>
[Route("ebusinessseckills/seckillorder")]
public class SeckillOrderController : AbpController
{
    public IOrderService orderService { get; set; }

    public ISeckillService seckillService { get; set; }

    public SeckillOrderController()
    {

    }

    /// <summary>
    /// 创建预定订单接口
    /// </summary>
    /// <param name="createSeckillPre"></param>
    /// <returns></returns>
    [HttpPost("SeckillPreOrder")]
    public async Task<SeckillPreOrderDto> CreateSeckillPreOrder([FromBody] CreateSeckillPreOrderDto createSeckillPre)
    {
        // 1、创建SeckillPreOrderDto
        SeckillPreOrderDto seckillPreOrderDto = ObjectMapper.Map<CreateSeckillPreOrderDto, SeckillPreOrderDto>(createSeckillPre); ;
        // 2、准备SeckillPreOrderDto
        //seckillPreOrderDto.ProductUrl = createSeckillPre.ProductUrl;
        //seckillPreOrderDto.ProductTitle = createSeckillPre.ProductTitle;
        //seckillPreOrderDto.ProductPrice = createSeckillPre.ProductPrice;
        //seckillPreOrderDto.ProductCount = createSeckillPre.ProductCount;
        seckillPreOrderDto.OrderTotalPrice = createSeckillPre.ProductPrice * createSeckillPre.ProductCount;

        // 3、返回SeckillPreOrderDto
        return seckillPreOrderDto;
    }

    /// <summary>
    /// 创建秒杀订单接口
    /// </summary>
    /// <param name="createSeckillOrderDto"></param>
    /// <returns></returns>
    [HttpPost("SeckillOrder")]
    public async Task<SeckillOrderDto> CreateSeckillOrder([FromBody] CreateSeckillOrderDto createSeckillOrderDto)
    {
        // 1、创建秒杀订单
        // 1.1、创建订单编号
        string OrderSn = OrderHelper.CreateOrderSn();
        //OrderItemDto orderItemDto = new OrderItemDto();
        //orderItemDto.Id = Guid.NewGuid();
        //orderItemDto.ItemCount = createSeckillOrderDto.ProductCount;
        //orderItemDto.ProductName = createSeckillOrderDto.ProductTitle;
        //orderItemDto.ItemPrice = createSeckillOrderDto.ProductPrice;
        //orderItemDto.ProductId = createSeckillOrderDto.ProductId;
        //orderItemDto.ProductUrl = createSeckillOrderDto.ProductUrl;
        //orderItemDto.ItemTotalPrice = createSeckillOrderDto.ProductPrice;
        OrderItemDto orderItemDto = ObjectMapper.Map<CreateSeckillOrderDto, OrderItemDto>(createSeckillOrderDto);
        orderItemDto.OrderSn = OrderSn;
        orderItemDto.ProductName = createSeckillOrderDto.ProductTitle;
        List<OrderItemDto> orderItemDtos = new List<OrderItemDto>();
        orderItemDtos.Add(orderItemDto);

        // 1.3、准备订单
        OrderDto orderDto = new OrderDto();
        //orderDto.Id = Guid.NewGuid();
        orderDto.OrderSn = OrderSn;
        orderDto.UserId = createSeckillOrderDto.UserId;
        orderDto.OrderTotalPrice = createSeckillOrderDto.OrderTotalPrice;
        orderDto.OrderStatus = 1;
        orderDto.OrderType = "";
        orderDto.OrderRemark = "";
        orderDto.OrderTel = "";
        orderDto.OrderName = "";
        orderDto.OrderAddress = "";
        orderDto.OrderItems = orderItemDtos;

        // 1.4、创建订单
        await orderService.CreateAsync(orderDto);

        // 2、扣减秒杀商品库存
        // 2.1、准备扣减秒杀商品库存Dto
        UpdateSeckillProductStockDto updateSeckillProductStockDto = new UpdateSeckillProductStockDto(); ;
        updateSeckillProductStockDto.ProductCount = createSeckillOrderDto.ProductCount;
        updateSeckillProductStockDto.SeckillProductId = createSeckillOrderDto.SeckillProductId;

        // 2.2、扣减秒杀库存
        await seckillService.UpdateSeckillProductStockAsync(updateSeckillProductStockDto);

        return new SeckillOrderDto()
        {
            OrderSn = OrderSn,
            OrderTotalPrice = createSeckillOrderDto.OrderTotalPrice,
            ProductName = createSeckillOrderDto.ProductTitle
        };
    }
}
